<?php
class Item extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->check_isvalidated();
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		$this->load->model('request_model');
		$this->load->model('inventory_model');
		$request_count = $this->request_model->get_request_count();
		$restock_count = $this->inventory_model->get_restock_count();
		$data['request_count'] = $request_count;
		$data['restock_count'] = $restock_count;
	}
	public function index()
	{
		$data = get_initial_data($this);
		$data['title'] = 'add item';
		$this->load->model('item_type_model');
		$data['item_type'] = $this->item_type_model->get_all();
		$data['file_error'] = '';
		$this->load->view('templates/header', $data);
		$this->load->view('item/addItem', $data);
		$this->load->view('templates/footer', $data);
	}
	public function add()
	{
		$is_file = 0;
		$file_name = '';
		if (isset($_FILES['userfile']) && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
			$file_name = now().'.txt';
			$config['upload_path'] = './uploads/items/';
			$config['allowed_types'] = 'txt';
			$config['max_size']	= '4000';
			$config['file_name']= $file_name;
			$this->upload->initialize($config);
			if ( ! $this->upload->do_upload())
			{
				$error = get_initial_data($this);
				$error['title'] = 'add item';
				$error = array('file_error' => $this->upload->display_errors());
				$this->load->model('item_type_model');
				$error['item_type'] = $this->item_type_model->get_all();
				$this->load->view('templates/header', $error);
				$this->load->view('item/addItem', $error);
				$this->load->view('templates/footer', $error);
				return;
			}
			else
			{
				$is_file = 1;
			}
		}
		$this->load->model('add_item_model');
		$error = $this->add_item_model->add_item($is_file, $file_name);
		$data = get_initial_data($this);
		$data['title'] = 'add item';
		$data['fail_items'] = $error;
		$this->load->model('item_type_model');
		$data['item_type'] = $this->item_type_model->get_all();
		$this->load->view('templates/header', $data);
		$this->load->view('item/index', $data);
		$this->load->view('templates/footer', $data);
	}

	public function updateItem(){
		if ($this->input->post('item_id') == null){
			redirect('item/update');
		}
		$data = get_initial_data($this);
		$data['title']='Update Item';
		$this->form_validation->set_rules('item_barcode', 'item_barcode',  'trim|required|exact_length[8]|xss_clean');
		$this->form_validation->set_rules('item_name', 'item_name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('manufacturer_name', 'manufacturer_name',  'trim|required|xss_clean|callback_check_manufacturer');
		$this->form_validation->set_rules('category_name', 'category_name', 'trim|required|xss_clean|callback_check_category');
		$this->form_validation->set_rules('item_type', 'item_type',  'trim|required|xss_clean');
		$this->form_validation->set_rules('curr_stock', 'curr_stock', 'trim|required|xss_clean|integer|greater_than[-1]');
		$this->form_validation->set_rules('cost', 'cost', 'trim|required|xss_clean|numeric|greater_than[-1]');
		$this->form_validation->set_rules('price', 'price', 'trim|required|xss_clean|numeric|greater_than[-1]');
		$this->form_validation->set_rules('min_stock', 'min_stock',  'trim|required|xss_clean|integer|greater_than[-1]');
		$this->form_validation->set_rules('promotion_unit', 'promotion_unit', 'trim|required|xss_clean|integer|greater_than[-1]');
		$this->form_validation->set_rules('promotion_discount', 'promotion_discount',  'trim|required|xss_clean|integer|greater_than[-1]');
		if ($this->form_validation->run()){
			$this->load->model('update_item_model');
			$this->update_item_model->update_item();
			$data['message'] = Messages::success_update_item;
			$this->load->view('templates/header',$data);
			$this->load->view('message',$data);
			$this->load->view('templates/footer',$data);
		}else{
			$id = $this->input->post('item_id');
			$this->load->model('item_model');
			$result = $this->item_model->get_item_by_id($id);
			if ($result != null){
				$this->load->model('item_type_model');
				$data['item_type'] = $this->item_type_model->get_all();
				$data['result'] = $result;
				$this->load->view('templates/header',$data);
				$this->load->view('item/update',$data);
				$this->load->view('templates/footer',$data);
				return;
			}else
				redirect('item/update/'.$id);
		}
	}

	public function check_category($str){
		$this->load->model('category_model');
		if ($this->category_model->get_id($str) == -1){
			$this->form_validation->set_message('check_category',  Messages::not_found_category);
			return false;
		}
		return true;
	}

	public function check_manufacturer($str){
		$this->load->model('manufacturer_model');
		if ($this->manufacturer_model->get_id($str) == -1){
			$this->form_validation->set_message('check_manufacturer', Messages::not_found_manufacturer);
			return false;
		}
		return true;
	}

	public function update($id)
	{
		$data = get_initial_data($this);
		if ($id != null){
			$data['title']='Update Item';
			$this->load->model('item_model');
			$result = $this->item_model->get_item_by_id($id);
			if ($result != null){
				$this->load->model('item_type_model');
				$data['item_type'] = $this->item_type_model->get_all();
				$data['result'] = $result;
				$this->load->view('templates/header',$data);
				$this->load->view('item/update',$data);
				$this->load->view('templates/footer',$data);
				return;
			}
		}
		$data['message'] = Messages::unable_find_item;
		$this->load->view('templates/header',$data);
		$this->load->view('message',$data);
		$this->load->view('templates/footer',$data);
	}

	public function search(){
		$data = get_initial_data($this);
		$name = $this->input->get('name');
		$category  = $this->input->get('category');
		$barcode = $this->input->get('barcode');
		$manufacturer = $this->input->get('manufacturer');
		$search = $this->input->get('search');
		$page = $this->input->get('per_page');
		$this->load->model('item_model');

		if($name || $category || $barcode|| $manufacturer)
			$data['name'] = $name;
		else
			$data['name'] = TRUE;
		$data['category'] = $category;
		$data['barcode'] = $barcode;
		$data['manufacturer'] = $manufacturer;
		$data['search'] = $search;
		$per_page = 20;

		$config = array();
		$config["base_url"] = site_url() . '/item/search?name='.$name.'&category='.$category.'&barcode='.$barcode.'&manufacturer='.$manufacturer.'&search='.$search;
		$config["total_rows"] = $this->item_model->count_search($search, $name, $barcode, $category, $manufacturer);
		$config["per_page"] = $per_page;
		$config['page_query_string'] = TRUE;
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		$data["links"] = $this->pagination->create_links();
		$data['result'] = $this->item_model->search($search, $name, $barcode, $category, $manufacturer, $per_page, $page);
			
		$data['title'] = 'item';
		$this->load->view('templates/header', $data);
		$this->load->view('item/search', $data);
		$this->load->view('templates/footer', $data);
		return;

		// 		$data['result'] = null;

		// 		$data['name'] = TRUE;
		// 		$data['category'] = FALSE;
		// 		$data['barcode'] = FALSE;
		// 		$data['manufacturer'] = FALSE;
		// 		$data['title'] = 'item';
		// 		$this->load->view('templates/header', $data);
		// 		$this->load->view('item/search', $data);
		// 		$this->load->view('templates/footer', $data);
	}

	public function restock(){
		$data = get_initial_data($this);
		$values = $this->input->post(NULL, TRUE);
		if ($values){
			$count = 0;
			$this->load->model('inventory_model');
			$this->load->model('price_rule_model');
			$this->load->model('promotion_model');
			foreach($values as $id => $stock){
				$stock = (int)$stock;
				if ($stock>0){
					$inventory = $this->inventory_model->get_inventory_by_item_id($id);
					if ($this->inventory_model->increase_stock($id, $stock)){
						$percent= ($inventory->inventory_current_stock-$inventory->inventory_min_stock+$stock)/$inventory->inventory_min_stock*100;
						$discount = $this->price_rule_model->get_discount($percent);
						$this->promotion_model->update_discount($id, $discount);
						if($stock >0)
							$count ++;
					}
				}
			}
			$data['title'] = 'restock';
			$data['message'] = $count . ' have been restocked';
			$this->load->view('templates/header',$data);
			$this->load->view('message',$data);
			$this->load->view('templates/footer',$data);
			return;
		}
		$this->load->model('item_model');

		$name = $this->input->get('name');
		$category  = $this->input->get('category');
		$barcode = $this->input->get('barcode');
		$manufacturer = $this->input->get('manufacturer');
		$search = $this->input->get('search');
		if ($search != null || strlen($search) > 0 )
			$data['result'] = $this->item_model->search($search, $name, $barcode, $category, $manufacturer);
		else
			$data['result'] = $this->item_model->get_low_stock_item();
		if($name || $category || $barcode|| $manufacturer)
			$data['name'] = $name;
		else
			$data['name'] = TRUE;
		$data['category'] = $category;
		$data['barcode'] = $barcode;
		$data['manufacturer'] = $manufacturer;
		$data['search'] = $search;
		$data['title'] = 'Restock';
		$this->load->view('templates/header', $data);
		$this->load->view('item/restock', $data);
		$this->load->view('templates/footer', $data);
	}

	private function check_isvalidated(){
		if(!$this->session->userdata('validated')){
			redirect('');
		}
	}
}